package org.apache.james.mime4j.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class StringArrayMap
  implements Serializable
{
  private static final long serialVersionUID = -196203035L;
  private final Map map = new HashMap();

  public static Map asMap(Map paramMap)
  {
    Iterator localIterator = paramMap.entrySet().iterator();
    while (localIterator.hasNext())
    {
      Map.Entry localEntry = (Map.Entry)localIterator.next();
      localEntry.setValue(asStringArray(localEntry.getValue()));
    }
    return Collections.unmodifiableMap(paramMap);
  }

  public static String asString(Object paramObject)
  {
    String str;
    if (paramObject == null)
      str = null;
    while (true)
    {
      while (true)
      {
        while (true)
        {
          return str;
          if (!(paramObject instanceof String))
            break;
          str = (String)paramObject;
        }
        if (!(paramObject instanceof String[]))
          break;
        str = ((String[])(String[])paramObject)[0];
      }
      if (!(paramObject instanceof List))
        break;
      str = (String)((List)paramObject).get(0);
    }
    throw new IllegalStateException("Invalid parameter class: " + paramObject.getClass().getName());
  }

  public static String[] asStringArray(Object paramObject)
  {
    String[] arrayOfString;
    if (paramObject == null)
      arrayOfString = null;
    while (true)
    {
      while (true)
      {
        while (true)
        {
          return arrayOfString;
          if (!(paramObject instanceof String))
            break;
          arrayOfString = new String[1];
          arrayOfString[0] = ((String)paramObject);
        }
        if (!(paramObject instanceof String[]))
          break;
        arrayOfString = (String[])(String[])paramObject;
      }
      if (!(paramObject instanceof List))
        break;
      List localList = (List)paramObject;
      arrayOfString = (String[])(String[])localList.toArray(new String[localList.size()]);
    }
    throw new IllegalStateException("Invalid parameter class: " + paramObject.getClass().getName());
  }

  public static Enumeration asStringEnum(Object paramObject)
  {
    Object localObject;
    if (paramObject == null)
      localObject = null;
    while (true)
    {
      while (true)
      {
        while (true)
        {
          return localObject;
          if (!(paramObject instanceof String))
            break;
          localObject = new 1(paramObject);
        }
        if (!(paramObject instanceof String[]))
          break;
        localObject = new 2((String[])(String[])paramObject);
      }
      if (!(paramObject instanceof List))
        break;
      localObject = Collections.enumeration((List)paramObject);
    }
    throw new IllegalStateException("Invalid parameter class: " + paramObject.getClass().getName());
  }

  protected void addMapValue(Map paramMap, String paramString1, String paramString2)
  {
    Object localObject = paramMap.get(paramString1);
    if (localObject == null)
      localObject = paramString2;
    while (true)
    {
      while (true)
      {
        while (true)
        {
          paramMap.put(paramString1, localObject);
          return;
          if (!(localObject instanceof String))
            break;
          ArrayList localArrayList1 = new ArrayList();
          localArrayList1.add(localObject);
          localArrayList1.add(paramString2);
          localObject = localArrayList1;
        }
        if (!(localObject instanceof List))
          break;
        ((List)localObject).add(paramString2);
      }
      if (!(localObject instanceof String[]))
        break;
      ArrayList localArrayList2 = new ArrayList();
      String[] arrayOfString = (String[])(String[])localObject;
      for (int i = 0; i < arrayOfString.length; ++i)
        localArrayList2.add(arrayOfString[i]);
      localArrayList2.add(paramString2);
      localObject = localArrayList2;
    }
    throw new IllegalStateException("Invalid object type: " + localObject.getClass().getName());
  }

  public void addValue(String paramString1, String paramString2)
  {
    addMapValue(this.map, convertName(paramString1), paramString2);
  }

  protected String convertName(String paramString)
  {
    return paramString.toLowerCase();
  }

  public Map getMap()
  {
    return asMap(this.map);
  }

  public String[] getNameArray()
  {
    Set localSet = this.map.keySet();
    return ((String[])(String[])localSet.toArray(new String[localSet.size()]));
  }

  public Enumeration getNames()
  {
    return Collections.enumeration(this.map.keySet());
  }

  public String getValue(String paramString)
  {
    return asString(this.map.get(convertName(paramString)));
  }

  public Enumeration getValueEnum(String paramString)
  {
    return asStringEnum(this.map.get(convertName(paramString)));
  }

  public String[] getValues(String paramString)
  {
    return asStringArray(this.map.get(convertName(paramString)));
  }
}